package com.turing.data.scripts.zjtq.filter

import com.turing.common.util.FileUtils
import com.turing.common.util.Log
import com.turing.common.util.StringUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import com.turing.data.engine.utils.RegexExtract

/**
 * 场景名称：追加提取单
 * 要素名称：产品名称
 **/

class ProductName_30228_34_filter {


    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        List<TaskElementVO.DrawResultVO> drawResultList = getData(resultVOs , dependParams)
        if(drawResultList == null ||
           (drawResultList.size() == 1 && StringUtils.isEmpty(drawResultList.get(0).getBusinessValue()))
        ){
            List<TaskElementVO.DrawResultVO> trueValueSet = new ArrayList<>()
            String REGEX = /(?<=有限公司：).{1,30}(?=委托人)/
            dependParams.JSONTextMap.forEach(
                    { childTaskSn, jsonText ->
                        RegexExtract.doRuleExtract(REGEX,childTaskSn, dependParams, jsonText, trueValueSet)
                    }
            )
            if(trueValueSet.size() > 0){
                return trueValueSet
            }
        }

        return drawResultList


    }

    static List< TaskElementVO.DrawResultVO > convertData(DependParamsVO dependParams , List<TaskElementVO.DrawResultVO> drawResultList , String businessValue){
        if(drawResultList.size() == 1 && StringUtils.isEmpty(businessValue) ){
            List<TaskElementVO.DrawResultVO> trueValueSet = new ArrayList<>()
//            String REGEX = /(?<=人民币.{1,10})\d+(?=】元)/
            String REGEX = /(?<=有限公司：).{1,30}(?=委托人)/
            dependParams.JSONTextMap.forEach(
                    { childTaskSn, jsonText ->
                        RegexExtract.doRuleExtract(REGEX,childTaskSn, dependParams, jsonText, trueValueSet)
                    }
            )
            if(trueValueSet.size() > 0){
                Log.info("ProductName_11436_34_filter_trueValueSet="+trueValueSet)
                return trueValueSet
            }
        }

        return drawResultList

    }

    static List< TaskElementVO.DrawResultVO > getData(List< TaskElementVO.DrawResultVO > resultVOs, DependParamsVO dependParams){
        if (resultVOs!=null && resultVOs.size()>0) {
            List<TaskElementVO.DrawResultVO > defaultResultVOs = new ArrayList<>()
            TaskElementVO.DrawResultVO resultVO = resultVOs.get(0)
//            Log.info("ProductName_11436_34_filter_resultVO="+resultVO)
            String text = resultVO.getText()
            String childTaskSn = resultVO.childTaskSn
            if(StringUtils.isEmpty(childTaskSn)){
                return null
            }
            int start = resultVO.start
            int end = resultVO.end
            def textPathMap = dependParams.textPathMap
            def jSONTextMap = dependParams.JSONTextMap
//            Log.info("ProductName_11436_34_filter_jSONTextMap="+jSONTextMap)
            def rawPath = textPathMap.get(childTaskSn)
            String rawText = FileUtils.loadJson(rawPath)
//            Log.info("ProductName_11436_34_filter_jsonData="+rawText)
            //往前补全到冒号的地方
            int backOffset = 10
            for(int i = 1 ; i < backOffset ; i++){
                String subText = rawText.substring(start - i , end)
//                Log.info("ProductName_11436_34_filter_subText="+subText)
                if(subText.contains("：") && !subText.contains("《")){
                    subText = subText.replace("：","")
                    resultVO.businessValue = subText
                    resultVO.type = 1
                    break;
                }
            }

            if (text.contains("资产管理合同")) {
                resultVO.setText(text.replace("资产管理合同", ""))
                resultVO.setEnd(resultVO.getEnd()-6)

                try {
                    int[] posInfo = resultVO.getPos_info()
                    posInfo[1] = posInfo[1] - 6
                    resultVO.setPos_info(posInfo as List<Integer>)

                    int[] globalInfo = resultVO.getGlobal_pos()
                    globalInfo[1] = globalInfo[1]-6
                    resultVO.setGlobal_pos(globalInfo as List<Integer>)
                }catch(Exception e){
                    e.printStackTrace()
                }


            }
            defaultResultVOs.add(resultVOs.get(0))
//            Log.info("ProductName_11436_34_filter_defaultResultVOs="+defaultResultVOs)
            return defaultResultVOs
        }

        return  resultVOs
    }

}
